package com.renjun.smweb.service.NewsService.impl;

import com.alibaba.fastjson.JSONObject;
import com.renjun.smweb.commons.componet.BaseDaoComponent;
import com.renjun.smweb.commons.enums.EOperators;
import com.renjun.smweb.dao.bean.InsertParams;
import com.renjun.smweb.dao.bean.Parameter;
import com.renjun.smweb.dao.bean.QueryParams;
import com.renjun.smweb.dao.bean.UpdateParams;
import com.renjun.smweb.dao.pojo.LoginInfo;
import com.renjun.smweb.exceptions.BaseException;
import com.renjun.smweb.service.NewsService.NewsService;
import com.renjun.smweb.commons.componet.BaseDaoComponent;
import com.renjun.smweb.commons.enums.EOperators;
import com.renjun.smweb.commons.util.*;
import com.renjun.smweb.dao.bean.InsertParams;
import com.renjun.smweb.dao.bean.Parameter;
import com.renjun.smweb.dao.bean.QueryParams;
import com.renjun.smweb.dao.pojo.LoginInfo;
import com.renjun.smweb.exceptions.BaseException;
import com.renjun.smweb.service.NewsService.NewsService;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * Created by lrs on 2017/2/28.
 */
@Service
public class NewsServiceImpl implements NewsService {

    @Autowired
    private BaseDaoComponent baseDaoComponent;


    @Override
    public JSONObject addNews(Map<String, Object> params) throws SQLException, BaseException {
        LoginInfo loginInfo = (LoginInfo) params.remove("userinfo");
        //参数校验
        validateNewsParams(params);
        //添加公告
        params.put("id", UUID.randomUUID().toString());
        params.put("status", "0");
        params.put("adduserid", loginInfo.getId());
        params.put("addtime", Format.getDate());
        params.put("athour", "系统管理员");
        InsertParams insertNews = InsertParams.createInsertParams("t_newsInfo", params);
        try {
            insertNews.setDbLog(loginInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        baseDaoComponent.insertDataByParams(insertNews);
        return ResponseUtils.createSuccessResponseBody("添加成功");
    }

    @Override
    public JSONObject modifyNews(Map<String, Object> params) throws SQLException, BaseException, ParseException {
        LoginInfo loginInfo = (LoginInfo) params.remove("userinfo");
        CollectionUtils.removeMapValueByKeys(params, "ename");
        //参数校验
        String id = (String) params.get("id");
        if (BaseChecks.hasEmptyStr(id)) throw new BaseException("系统公告id不能为空");
        validateNewsParams(params);

        //查询系统公告
        QueryParams clsParams = QueryParams.createQueryParams("T_NEWSINFO");
        clsParams.addQueryParams(Parameter.createParameter("id", params.get("id")));
        try {
            clsParams.setDbLog(loginInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        List<Map<String, Object>> news = baseDaoComponent.selectDataByParams(clsParams);

        //放入修改人参数
        params.put("updateuserid", loginInfo.getId());
        params.put("updatetime", Format.getDateTime());

        //获取系统公告的旧时间
        Date openTime = DateUtils.parseDate((String) news.get(0).get("startTime"), new String[]{"yyyy-MM-dd"});
        Date closeTime = DateUtils.parseDate((String) news.get(0).get("endTime"), new String[]{"yyyy-MM-dd"});

        //获取系统公告的新时间
        Date startTime = DateUtils.parseDate((String) params.get("starttime"), new String[]{"yyyy-MM-dd"});
        Date endTime = DateUtils.parseDate((String) params.get("endtime"), new String[]{"yyyy-MM-dd"});
        // 大于0表示前一个数据比后一个数据大， 0表示相等，小于0表示第一个数据小于第二个数据

        //比较时间
        if ((startTime.compareTo(openTime) >= 0 || startTime.compareTo(closeTime) < 0)
                || (endTime.compareTo(openTime) >= 0 || endTime.compareTo(closeTime) < 0)) {

            //修改系统公告
            UpdateParams updateNews = UpdateParams.createUpdateParams("t_newsInfo");
            updateNews.addWhereParameter(Parameter.createParameter("id", params.get("id")));
            updateNews.addParamsForMap(params);
            try {
                updateNews.setDbLog(loginInfo);
            } catch (Exception e) {
                e.printStackTrace();
            }
            baseDaoComponent.updateDataByParams(updateNews);
        } else {
            throw new BaseException("不在设定时间范围内，不能进行修改");
        }
        return ResponseUtils.createSuccessResponseBody("修改成功");
    }

    @Override
    public JSONObject queryNews(Map<String, Object> params) throws SQLException {
        LoginInfo loginInfo = (LoginInfo) params.remove("userinfo");
        //查询
        QueryParams queryNews = QueryParams.createQueryParams("vb_queryNews_lrs");
        String title = (String) params.remove("title");    //标题
        //公告标题 模糊查询
        if(!BaseChecks.hasEmptyStr(title)){
            title = StringUtils.removeEmptyCharsOfStr(title);
            queryNews.addQueryParams(Parameter.createParameter("title", EOperators.类似, title));
        }
        //公告发布人 模糊查询
        String ename = (String) params.remove("ename");    //发布人
        if(!BaseChecks.hasEmptyStr(ename)){
            ename = StringUtils.removeEmptyCharsOfStr(ename);
            queryNews.addQueryParams(Parameter.createParameter("ename", EOperators.类似, ename));
        }
        String starttime = (String) params.remove("starttime");    //开始时间
        String endtime = (String) params.remove("endtime");        //结束时间
        //时间范围查询
        if(!BaseChecks.hasEmptyStr(starttime)){
            queryNews.addQueryParams(Parameter.createParameter("starttime", EOperators.小于等于, starttime));
        }
        if(!BaseChecks.hasEmptyStr(endtime)){
            queryNews.addQueryParams(Parameter.createParameter("endtime", EOperators.大于等于, endtime));
        }
        queryNews.addQueryParamsByMap(CollectionUtils.removeEmptyKeyByParams(params));
        queryNews.addOrderColumns("addtime desc");
        try {
            queryNews.setDbLog(loginInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        List<Map<String, Object>> mapList = baseDaoComponent.selectDataByParams(queryNews);
        return ResponseUtils.createSuccessResponseBody("查询成功", mapList);
    }

    @Override
    public JSONObject getNews(Map<String, Object> params) throws SQLException, BaseException {
        LoginInfo loginInfo = (LoginInfo) params.remove("userinfo");
        //参数校验
        String id = (String) params.get("id");
        if (BaseChecks.hasEmptyStr(id)) throw new BaseException("系统公告id不能为空");
        //查询
        QueryParams queryNews = QueryParams.createQueryParams("vb_queryNews_lrs");
        queryNews.addQueryParams(Parameter.createParameter("id", id));
        try {
            //记录日志
            queryNews.setDbLog(loginInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        List<Map<String, Object>> mapList = baseDaoComponent.selectDataByParams(queryNews);
        return ResponseUtils.createSuccessResponseBody("查询成功", mapList.get(0));
    }

    @Override
    public JSONObject cancelNews(Map<String, Object> params) throws SQLException, BaseException {
        LoginInfo loginInfo = (LoginInfo) params.remove("userinfo");
        //参数校验
        String id = (String) params.get("id");
        if (BaseChecks.hasEmptyStr(id)) throw new BaseException("系统公告id不能为空");
        //注销
        UpdateParams cancelNews = UpdateParams.createUpdateParams("t_newsInfo");
        cancelNews.addWhereParameter(Parameter.createParameter("id", id));
        cancelNews.addParams(Parameter.createParameter("status", "1"));
        try {
            cancelNews.setDbLog(loginInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        baseDaoComponent.updateDataByParams(cancelNews);
        return ResponseUtils.createSuccessResponseBody("注销成功");
    }

    @Override
    public JSONObject recoverNews(Map<String, Object> params) throws SQLException, BaseException {
        LoginInfo loginInfo = (LoginInfo) params.remove("userinfo");
        //参数校验
        String id = (String) params.get("id");
        if (BaseChecks.hasEmptyStr(id)) throw new BaseException("系统公告id不能为空");
        //恢复
        UpdateParams recoverNews = UpdateParams.createUpdateParams("t_newsInfo");
        recoverNews.addWhereParameter(Parameter.createParameter("id", id));
        recoverNews.addParams(Parameter.createParameter("status", "0"));
        try {
            recoverNews.setDbLog(loginInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        baseDaoComponent.updateDataByParams(recoverNews);
        return ResponseUtils.createSuccessResponseBody("恢复成功");
    }

    @Override
    public JSONObject loginQueryNewsList(Map<String, Object> params) throws SQLException {
        LoginInfo loginInfo = (LoginInfo) params.remove("userinfo");
        //查询
        String date = Format.getDate();
        QueryParams queryParams = QueryParams.createQueryParams("vb_queryNews_lrs");
        //当天大于等于系统公告开始时间，小于等于系统公告的结束时间
        queryParams.addQueryParams(Parameter.createParameter("STARTTIME", EOperators.小于等于,date));
        queryParams.addQueryParams(Parameter.createParameter("ENDTIME", EOperators.大于等于,date));
        queryParams.addQueryParams(Parameter.createParameter("status","0"));
        queryParams.addOrderColumns(" addtime DESC ");
        try {
            queryParams.setDbLog(loginInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        List<Map<String, Object>> result = baseDaoComponent.selectDataByParams(queryParams);
        return ResponseUtils.createSuccessResponseBody("查询成功", result);
    }


    //验证公告表的参数
    private void validateNewsParams(Map<String, Object> params) throws BaseException {
        String title = (String) params.get("title");
        if (BaseChecks.hasEmptyStr(title)) throw new BaseException("公告标题不能为空");

        String starttime = (String) params.get("starttime");
        if (BaseChecks.hasEmptyStr(starttime)) throw new BaseException("公告开始时间不能为空");

        String endtime = (String) params.get("endtime");
        if (BaseChecks.hasEmptyStr(endtime)) throw new BaseException("公告结束时间不能为空");

        String content = (String) params.get("content");
        if (BaseChecks.hasEmptyStr(content)) throw new BaseException("公告内容不能为空");
    }
}
